package com.tgy.binarysearch;



import java.util.Objects;

/**
 * @Author: tgy
 * @Date: 2020-11-17 15:29
 *
 * 1）二分查找元素value的下标，如无 return -1
 */
public class BSEqualValue {

    public int binarySearch(Integer[] nums, int value) {

        if (Objects.isNull(nums) || nums.length == 0) {

            return -1;
        }

        int left = 0, right = nums.length,mid;

        while (left != right) {

            mid = (left + right) >> 1;

            if (value > nums[mid]) {

                left = mid + 1;
            }else if (value < nums[mid]) {

                right = mid;
            }else {

                return mid;
            }
        }

        return -1;
    }
}
